extends Sprite2D

@export var grid:TileMap
var target_path : Array[Vector2i]


func _process(delta: float) -> void:
	if Input.is_action_just_pressed("click_move"):
		var mouse_position = get_global_mouse_position()
		
		var start_coord = grid.local_to_map(global_position)
		if target_path and target_path.size()>0:
			start_coord = target_path[0]
		
		var target_coord = grid.local_to_map(mouse_position)
		
		var calculated_path = grid.astar.get_id_path(start_coord,target_coord)
		
		if calculated_path.size()>0:
			target_path = grid.astar.get_id_path(start_coord,target_coord)
		
	
	if target_path and not target_path.is_empty():
		var target_position = grid.map_to_local(target_path[0])
		global_rotation = global_position.direction_to(target_position).angle()
		
		global_position = global_position.move_toward(target_position,100*delta)
		
		if global_position ==target_position:
			target_path.remove_at(0)
	
	
	
